package com.zch.utils.snowflake;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 实现了AutoCloseable接口，try 模块执行完，或者try 代码块有异常都会自动执行close()方法。
   好处是省去了try-catch-finally 这种复杂写法
 */
public class ResourceLock extends ReentrantLock implements AutoCloseable {
    /**
     *
     * 获得锁
     * 实现了AutoCloseable接口，try 模块执行完，
     * 或者try 代码块有异常都会自动执行close()方法。
     * 好处是省去了try-catch-finally 这种复杂写法
     * @return this ResourceLock
     */
    public ResourceLock obtain() {
        lock();
        return this;
    }

    /**
     * Unlock the resource lock.
     *
     * <p>This is typically used in try-with-resources blocks to automatically
     * unlock the resource lock when the block is exited, regardless of whether
     * an exception is thrown or not.
     */
    @Override
    public void close() {
        this.unlock();
    }

}
